package com.leejxx.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leejxx.admin.dao.AdSensitiveDao;
import com.leejxx.admin.service.AdSensitiveService;
import com.leejxx.model.common.dto.PageResponseResult;
import com.leejxx.model.common.dto.ResponseResult;
import com.leejxx.model.admin.dtos.SensitiveDto;
import com.leejxx.model.common.enums.AppHttpCodeEnum;
import com.leejxx.model.admin.pojos.AdSensitive;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author LeeJx
 * @ClassName AdSensitiveServiceImpl
 * @Description TODO
 * @date 2021/2/19 16:42
 * @Version 1.0
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveDao, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult findByNameAndPage(SensitiveDto sensitiveDto) {
        if (null == sensitiveDto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitiveDto.checkParam();

        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, sensitiveDto.getName());
        }

        Page<AdSensitive> sensitivePage = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) sensitivePage.getTotal());
        responseResult.setData(sensitivePage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        if (null == adSensitive) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adSensitive.setCreatedTime(new Date());

        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        if (null == adSensitive || null == adSensitive.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteSensitive(String id) {
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdSensitive sensitive = getById(id);
        if (null == sensitive) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
